package com.polimi.meteocal.business.control;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import com.polimi.meteocal.business.entity.Ack;
import com.polimi.meteocal.business.entity.User;

@Stateless
@LocalBean
public class PartecipationManager {
	@PersistenceContext
    EntityManager em;
    
    public PartecipationManager() {}    
    
    public List<User> getPartecipants(int eventId){
    	List<User> partecipants = this.em
				.createNamedQuery(User.FIND_PARTECIPANTS_BY_EVENT_ID, User.class)
				.setParameter("eventId", eventId).getResultList();
    	return partecipants;
    }
    public void setPartecipation(Ack partecipation){		
		this.em.persist(partecipation);		
	}
    public Ack getPartecipation(int id, User sender, User receiver){
    	
    	try{
    	Ack partecipation = this.em
    			.createNamedQuery(Ack.FIND_ACK_FOR_INVITE, Ack.class)
				.setParameter("eventId", id)
				.setParameter("inviteReceiver", receiver.getEmail())
				.setParameter("inviteSender", sender.getEmail())
				.getSingleResult();
    	return partecipation;
    	}catch(NoResultException e){
    		return null;
    	}    					
    }
}
